//package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.service.PermissionService;
import com.example.e_comm.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/permissions")
public class PermissionController {
    private final PermissionService permissionService;
    private UserService userService;

    public PermissionController(PermissionService permissionService, UserService userService) {
        this.permissionService = permissionService;
        this.userService = userService;
    }

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @GetMapping("/list")
    public List<Permission> list() {
        return permissionService.list();
    }

    @GetMapping("/listByPaqe")
    public List<Permission> listByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        return permissionService.getPermissionsByPage(new Page<>(pageNum, pageSize));
    }

    @GetMapping("/listByPageWithCondition")
    public List<Permission> listByPageWithCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword
    ) {
        Page<Permission> page = new Page<>(pageNum, pageSize);
        return permissionService.getPermissionsByPageWithCondition(page, keyword);
    }

    @PostMapping("/create")
    public boolean create(@RequestBody Permission permission) {
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_name", permission.getPermName())) > 0) {
            return false;
        }
        return permissionService.save(permission);
    }

    @PostMapping("/delete/ids")
    public boolean delete(@RequestBody List<Long> ids) {
        return permissionService.removeByIds(ids);
    }

    @GetMapping("/listPermissionByUserId")
    public List<Permission> getPermissionByUserId(@RequestParam Long userId) {
        if (permissionService.getById(userId) == null) {
            throw new RuntimeException("用户不存在");
        }
        return permissionService.getPermissionsByUserId(userId);
    }

    @GetMapping("/listPermissionByRoleId")
    public List<Permission> getPermissionByRoleId(@RequestParam Long roleId) {
        if (permissionService.getById(roleId) == null) {
            throw new RuntimeException("角色不存在");
        }
        return permissionService.getPermissionsByRoleId(roleId);
    }

    @GetMapping("/listPermissionIdsByUserId")
    public Set<String> getPermissionIdsByRoleId(@RequestParam Long userId) {
        if (userService.getById(userId) == null) {
            throw new RuntimeException("用户不存在");
        }
        return permissionService.getPermissionsByUserIdSet(userId);
    }
}